package cn.edu.zjgsu.ito.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.zjgsu.ito.dao.CourierOrderMapper;
import cn.edu.zjgsu.ito.dao.DemandOrderMapper;
import cn.edu.zjgsu.ito.dao.ImageUrlDemandOrderMapper;
import cn.edu.zjgsu.ito.dao.ImageUrlDemandOrderStoreMapper;
import cn.edu.zjgsu.ito.dao.OrderNoCourierMapper;
import cn.edu.zjgsu.ito.dao.OrderTableMapper;
import cn.edu.zjgsu.ito.dao.UserAddressMapper;
import cn.edu.zjgsu.ito.dao.UserCourierMapper;
import cn.edu.zjgsu.ito.dao.UserInfomationMapper;
import cn.edu.zjgsu.ito.dao.UserLoginMapper;
import cn.edu.zjgsu.ito.dao.UserRefundMapper;
import cn.edu.zjgsu.ito.dao.UserScoreMapper;
import cn.edu.zjgsu.ito.model.CourierOrder;
import cn.edu.zjgsu.ito.model.CourierOrderExample;
import cn.edu.zjgsu.ito.model.DemandOrder;
import cn.edu.zjgsu.ito.model.DemandOrderExample;
import cn.edu.zjgsu.ito.model.ImageUrlDemandOrder;
import cn.edu.zjgsu.ito.model.ImageUrlDemandOrderStore;
import cn.edu.zjgsu.ito.model.OrderNoCourier;
import cn.edu.zjgsu.ito.model.OrderNoCourierExample;
import cn.edu.zjgsu.ito.model.OrderTable;
import cn.edu.zjgsu.ito.model.OrderTableExample;
import cn.edu.zjgsu.ito.model.UserAddress;
import cn.edu.zjgsu.ito.model.UserAddressExample;
import cn.edu.zjgsu.ito.model.UserCourier;
import cn.edu.zjgsu.ito.model.UserCourierExample;
import cn.edu.zjgsu.ito.model.UserInfomation;
import cn.edu.zjgsu.ito.model.UserInfomationExample;
import cn.edu.zjgsu.ito.model.UserLogin;
import cn.edu.zjgsu.ito.model.UserRefund;
import cn.edu.zjgsu.ito.model.UserRefundExample;
import cn.edu.zjgsu.ito.model.UserScore;
import cn.edu.zjgsu.ito.service.UserManageServiceI;
import cn.edu.zjgsu.ito.util.GetTime;
import cn.edu.zjgsu.ito.vo.DemandOrderMessageVo;
import cn.edu.zjgsu.ito.vo.OrderIdVo;
import cn.edu.zjgsu.ito.vo.RefundReasonShowVo;
import cn.edu.zjgsu.ito.vo.UserAddressVo;
import cn.edu.zjgsu.ito.vo.UserDemandOrderShowVo;
import cn.edu.zjgsu.ito.vo.UserInfomationSaveVo;
import cn.edu.zjgsu.ito.vo.UserInfomationShowVo;
import cn.edu.zjgsu.ito.vo.UserPurchaseOrderVo;

@Service("userManageService")
public class UserManageServiceImpl implements UserManageServiceI {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@Autowired
	private UserInfomationMapper userInfomationMapper;

	@Autowired
	private UserAddressMapper userAddressMapper;

	@Autowired
	private DemandOrderMapper demandOrderMapper;

	@Autowired
	private OrderTableMapper orderTableMapper;

	@Autowired
	private OrderNoCourierMapper orderNoCourierMapper;

	@Autowired
	private CourierOrderMapper courierOrderMapper;

	@Autowired
	private UserCourierMapper userCourierMapper;

	@Autowired
	private UserScoreMapper userScoreMapper;

	@Autowired
	private ImageUrlDemandOrderMapper imageUrlDemandOrderMapper;
	
	@Autowired
	private ImageUrlDemandOrderStoreMapper imageUrlDemandOrderStoreMapper;
	
	@Autowired
	private UserRefundMapper userRefundMapper;
	
	@Autowired
	private UserLoginMapper userLoginMapper;
	
	@Override
	public List<UserDemandOrderShowVo> userDemandOrder(int userId) {

		List<UserDemandOrderShowVo> userDemandOrderShowVos = new ArrayList<UserDemandOrderShowVo>();
		DemandOrderExample example = new DemandOrderExample();
		example.or().andUserIdEqualTo(userId);
		List<DemandOrder> demandOrder = demandOrderMapper
				.selectByExample(example);
		for (int i = 0; i < demandOrder.size(); i++) {
			UserDemandOrderShowVo userDemandOrderShowVo = new UserDemandOrderShowVo();
			BeanUtils.copyProperties(demandOrder.get(i), userDemandOrderShowVo);
			userDemandOrderShowVo.setDemandCreatetime(GetTime
					.dateToStr(demandOrder.get(i).getDemandCreatetime()));
			
			
			OrderTableExample orderTableExample = new OrderTableExample();
			orderTableExample.or().andDemandIdEqualTo(demandOrder.get(i).getDemandId());
			List<OrderTable> orderTable = orderTableMapper.selectByExample(orderTableExample);
			if(orderTable.size() > 0){
				userDemandOrderShowVo.setOrderState(orderTable.get(0).getOrderState());
			}
			userDemandOrderShowVos.add(userDemandOrderShowVo);
		}
		return userDemandOrderShowVos;
	}

	@Override
	public List<UserDemandOrderShowVo> userChooseDemandOrder(int temp,
			int userId) {

		List<UserDemandOrderShowVo> userDemandOrderShowVos = new ArrayList<UserDemandOrderShowVo>();
		DemandOrderExample example = new DemandOrderExample();
		example.or().andUserIdEqualTo(userId);
		List<DemandOrder> demandOrder = demandOrderMapper.selectByExample(example);
		for (int i = 0; i < demandOrder.size(); i++) {
			UserDemandOrderShowVo userDemandOrderShowVo = new UserDemandOrderShowVo();
			BeanUtils.copyProperties(demandOrder.get(i), userDemandOrderShowVo);
			userDemandOrderShowVo.setDemandCreatetime(GetTime
					.dateToStr(demandOrder.get(i).getDemandCreatetime()));
			
			
			OrderTableExample orderTableExample = new OrderTableExample();
			orderTableExample.or().andDemandIdEqualTo(demandOrder.get(i).getDemandId());
			List<OrderTable> orderTable = orderTableMapper.selectByExample(orderTableExample);
			if(orderTable.size() > 0){
				userDemandOrderShowVo.setOrderState(orderTable.get(0).getOrderState());
			}
			if(temp == 0){
				if(userDemandOrderShowVo.getDemandState() == 0){
					userDemandOrderShowVos.add(userDemandOrderShowVo);
				}
			}else if(temp == 1){
				if(userDemandOrderShowVo.getOrderState() != 7 && userDemandOrderShowVo.getOrderState() != 6){
					userDemandOrderShowVos.add(userDemandOrderShowVo);
				}
			}else if(temp == 2){
				if(userDemandOrderShowVo.getOrderState() == 6 || userDemandOrderShowVo.getOrderState() == 7){
					userDemandOrderShowVos.add(userDemandOrderShowVo);
				}
			}else if(temp == 3){
				userDemandOrderShowVos.add(userDemandOrderShowVo);
			}
		}
		return userDemandOrderShowVos;
		
	}
	
	@Override
	public List<UserDemandOrderShowVo> userDemandOrderSelectBy(String temp,
			int userId) {
		List<UserDemandOrderShowVo> userDemandOrderShowVos = new ArrayList<UserDemandOrderShowVo>();
		DemandOrderExample example = new DemandOrderExample();
		example.or().andUserIdEqualTo(userId).andTradeNameLike("%" + temp + "%");
		List<DemandOrder> demandOrder = demandOrderMapper
				.selectByExample(example);
		for (int i = 0; i < demandOrder.size(); i++) {
			UserDemandOrderShowVo userDemandOrderShowVo = new UserDemandOrderShowVo();
			BeanUtils.copyProperties(demandOrder.get(i), userDemandOrderShowVo);
			userDemandOrderShowVo.setDemandCreatetime(GetTime
					.dateToStr(demandOrder.get(i).getDemandCreatetime()));
			OrderTableExample orderTableExample = new OrderTableExample();
			orderTableExample.or().andDemandIdEqualTo(demandOrder.get(i).getDemandId());
			List<OrderTable> orderTable = orderTableMapper.selectByExample(orderTableExample);
			if(orderTable.size() > 0){
				userDemandOrderShowVo.setOrderState(orderTable.get(0).getOrderState());
			}
			
			userDemandOrderShowVos.add(userDemandOrderShowVo);
		}
		return userDemandOrderShowVos;
	}
	
	@Override
	public List<UserPurchaseOrderVo> userPurchaseOrder(int userId) {
		List<UserPurchaseOrderVo> userPurchaseOrderVos = new ArrayList<UserPurchaseOrderVo>();

		OrderNoCourierExample example = new OrderNoCourierExample();
		example.or().andPurchaseUserIdEqualTo(userId);
		List<OrderNoCourier> orderNoCouriers = orderNoCourierMapper
				.selectByExample(example);

		for (int i = 0; i < orderNoCouriers.size(); i++) {
			UserPurchaseOrderVo userPurchaseOrderVo = new UserPurchaseOrderVo();

			BeanUtils.copyProperties(orderNoCouriers.get(i),
					userPurchaseOrderVo);

			userPurchaseOrderVo.setAcceptTime(GetTime.dateToStr(orderNoCouriers
					.get(i).getAcceptTime()));
			
			userPurchaseOrderVo.setTradeName(orderNoCouriers.get(i).getTradeName());
			
			userPurchaseOrderVos.add(userPurchaseOrderVo);
		}

		return userPurchaseOrderVos;

	}
	
	@Override
	public List<UserPurchaseOrderVo> userChoosePurchaseOrder(int temp,int userId) {
		List<UserPurchaseOrderVo> userPurchaseOrderVos = new ArrayList<UserPurchaseOrderVo>();

		OrderNoCourierExample example = new OrderNoCourierExample();
		example.or().andPurchaseUserIdEqualTo(userId);
		List<OrderNoCourier> orderNoCouriers = orderNoCourierMapper
				.selectByExample(example);

		for (int i = 0; i < orderNoCouriers.size(); i++) {
			UserPurchaseOrderVo userPurchaseOrderVo = new UserPurchaseOrderVo();

			BeanUtils.copyProperties(orderNoCouriers.get(i),
					userPurchaseOrderVo);

			userPurchaseOrderVo.setAcceptTime(GetTime.dateToStr(orderNoCouriers
					.get(i).getAcceptTime()));
			
			userPurchaseOrderVo.setTradeName(orderNoCouriers.get(i).getTradeName());
			if(temp == 0){
				if(userPurchaseOrderVo.getOrderState() != 6 && userPurchaseOrderVo.getOrderState() != 7){
					userPurchaseOrderVos.add(userPurchaseOrderVo);
				}
			}else if(temp == 1){
				if(userPurchaseOrderVo.getOrderState() == 6 || userPurchaseOrderVo.getOrderState() != 7){
					userPurchaseOrderVos.add(userPurchaseOrderVo);
				}
			}else if(temp == 2){
					userPurchaseOrderVos.add(userPurchaseOrderVo);
			}
			
		}

		return userPurchaseOrderVos;

	}
	
	
	@Override
	public List<UserPurchaseOrderVo> userPurchaseOrderSelectBy(String temp,
			int userId) {
		List<UserPurchaseOrderVo> userPurchaseOrderVos = new ArrayList<UserPurchaseOrderVo>();

		OrderNoCourierExample example = new OrderNoCourierExample();
		example.or().andPurchaseUserIdEqualTo(userId).andNicknameLike("%" + temp + "%");
		example.or().andPurchaseUserIdEqualTo(userId).andTradeNameLike("%" + temp + "%");
		List<OrderNoCourier> orderNoCouriers = orderNoCourierMapper
				.selectByExample(example);

		for (int i = 0; i < orderNoCouriers.size(); i++) {
			UserPurchaseOrderVo userPurchaseOrderVo = new UserPurchaseOrderVo();

			BeanUtils.copyProperties(orderNoCouriers.get(i),
					userPurchaseOrderVo);

			userPurchaseOrderVo.setAcceptTime(GetTime.dateToStr(orderNoCouriers
					.get(i).getAcceptTime()));

			userPurchaseOrderVos.add(userPurchaseOrderVo);
		}

		return userPurchaseOrderVos;
	}
	
	

	@Override
	public UserInfomationShowVo getInfomation(int userId) {
		UserInfomationShowVo userInfomationShowVo = new UserInfomationShowVo();

		UserInfomation userInfomation = userInfomationMapper
				.selectByPrimaryKey(userId);
		BeanUtils.copyProperties(userInfomation, userInfomationShowVo);

		// 复制用户分数属性
		UserScore userScore = userScoreMapper.selectByPrimaryKey(userId);
		BeanUtils.copyProperties(userScore, userInfomationShowVo);

		userInfomationShowVo = getCourierMessage(userId, userInfomationShowVo);

		return userInfomationShowVo;
	}

	@Override
	public UserInfomationSaveVo editUserInfomation(
			UserInfomationSaveVo userInfomationSaveVo, int userId) {

		userInfomationSaveVo.setUserId(userId);

		UserInfomation userInfomation = new UserInfomation();

		BeanUtils.copyProperties(userInfomationSaveVo, userInfomation);

		UserInfomationExample example = new UserInfomationExample();
		example.or().andUserIdEqualTo(userId);
		userInfomationMapper.updateByExampleSelective(userInfomation, example);
		// userInfomationMapper.updateByPrimaryKey(userInfomation);
		return userInfomationSaveVo;
	}

	@Override
	public List<UserAddressVo> getAddress(int userId) {

		List<UserAddressVo> userAddressVoes = new ArrayList<UserAddressVo>();
		UserAddressExample userAddressExample = new UserAddressExample();
		userAddressExample.or().andUserIdEqualTo(userId);
		List<UserAddress> userAddresses = userAddressMapper
				.selectByExample(userAddressExample);
		for (int i = 0; i < userAddresses.size(); i++) {
			UserAddressVo userAddressVo = new UserAddressVo();
			BeanUtils.copyProperties(userAddresses.get(i), userAddressVo);
			userAddressVoes.add(userAddressVo);
		}
		return userAddressVoes;
	}

	@Override
	public List<UserAddressVo> addAddress(UserAddressVo userAddressVo) {

		List<UserAddressVo> userAddressVos = new ArrayList<UserAddressVo>();

		UserAddress userAddress = new UserAddress();

		BeanUtils.copyProperties(userAddressVo, userAddress);

		userAddressMapper.insertSelective(userAddress);

		return userAddressVos;
	}

	@Override
	public List<UserAddressVo> deleteAddress(UserAddressVo userAddressVo) {

		userAddressMapper.deleteByPrimaryKey(userAddressVo.getAddressId());

		List<UserAddressVo> userAddressVoes = new ArrayList<UserAddressVo>();

		return userAddressVoes;
	}

	@Override
	public List<UserAddressVo> updateAddress(UserAddressVo userAddressVo) {

		UserAddress userAddress = new UserAddress();

		BeanUtils.copyProperties(userAddressVo, userAddress);

		userAddressMapper.updateByPrimaryKeySelective(userAddress);

		List<UserAddressVo> userAddressVoes = new ArrayList<UserAddressVo>();

		return userAddressVoes;
	}

	/*
	 * @Override public DemandOrderMessageVo userDemandOrderMessage(DemandIdVo
	 * demandIdVo) {
	 * 
	 * DemandOrderMessageVo demandOrderMessageVo = new DemandOrderMessageVo();
	 * 
	 * int orderId = copyDemandOrderMessage(demandOrderMessageVo,
	 * demandIdVo.getDemandId());
	 * 
	 * 
	 * if (orderId != -1) {
	 * 
	 * copyOrderMessageNoCourier(demandOrderMessageVo, orderId);
	 * 
	 * }
	 * 
	 * copyOrderCourierMessage(demandOrderMessageVo, orderId);
	 * 
	 * return demandOrderMessageVo; }
	 * 
	 * 
	 * 
	 * 
	 * @Override public DemandOrderMessageVo userPurchaseOrderMessage(OrderIdVo
	 * orderIdVo) {
	 * 
	 * DemandOrderMessageVo demandOrderMessageVo = new DemandOrderMessageVo();
	 * 
	 * copyOrderMessageNoCourier(demandOrderMessageVo,orderIdVo.getOrderId());
	 * 
	 * copyOrderCourierMessage(demandOrderMessageVo,orderIdVo.getOrderId());
	 * 
	 * return demandOrderMessageVo; }
	 */
	/**
	 * 复制求购订单信息
	 * 
	 * @param demandOrderMessageVo
	 * @param demandId
	 * @return
	 */
	public int copyDemandOrderMessage(
			DemandOrderMessageVo demandOrderMessageVo, int demandId) {

		DemandOrder demandOrder = new DemandOrder();
		DemandOrderExample example = new DemandOrderExample();
		example.or().andDemandIdEqualTo(demandId);
		demandOrder = demandOrderMapper.selectByPrimaryKey(demandId);

		OrderTableExample orderTableExample = new OrderTableExample();
		orderTableExample.or().andDemandIdEqualTo(demandId);
		List<OrderTable> orderTable = orderTableMapper
				.selectByExample(orderTableExample);

		BeanUtils.copyProperties(demandOrder, demandOrderMessageVo);

		String demandCreatetime = GetTime.dateToStr(demandOrder
				.getDemandCreatetime());
		demandOrderMessageVo.setDemandCreatetime(demandCreatetime);

		int orderId = -1;
		if (orderTable.size() > 0) {
			orderId = orderTable.get(0).getOrderId();
		}

		return orderId;
	}

	/**
	 * 复制求购单信息无快递
	 * 
	 * @param demandOrderMessageVo
	 * @param orderId
	 */
	public DemandOrderMessageVo copyOrderMessageNoCourier(
			DemandOrderMessageVo demandOrderMessageVo, int orderId) {

		OrderNoCourierExample example = new OrderNoCourierExample();
		example.or().andOrderIdEqualTo(orderId);
		List<OrderNoCourier> orderNoCourierList = orderNoCourierMapper
				.selectByExample(example);

		BeanUtils.copyProperties(orderNoCourierList.get(0),
				demandOrderMessageVo);

		String purchaseName = userInfomationMapper.selectByPrimaryKey(
				demandOrderMessageVo.getPurchaseUserId()).getNickname();
		String acceptTime = GetTime.dateToStr(orderNoCourierList.get(0)
				.getAcceptTime());

		demandOrderMessageVo.setPurchaseName(purchaseName);
		demandOrderMessageVo.setAcceptTime(acceptTime);

		return demandOrderMessageVo;
	}

	/**
	 * 复制订单具体信息服务
	 * 
	 * @param demandOrderMessageVo
	 * @param orderId
	 * @return
	 */
	public DemandOrderMessageVo copyOrderCourierMessage(
			DemandOrderMessageVo demandOrderMessageVo, int orderId) {

		CourierOrderExample example = new CourierOrderExample();
		example.or().andOrderIdEqualTo(orderId);
		List<CourierOrder> courierOrders = courierOrderMapper
				.selectByExample(example);

		if (courierOrders.size() == 0) {
			return demandOrderMessageVo;
		}

		int courierId = courierOrders.get(0).getCourierId();

		String arriveTime = GetTime.dateToStr(courierOrders.get(0)
				.getArriveTime());

		System.out.println("快递员id" + courierId);

		String courierName = getCourierName(courierId);

		demandOrderMessageVo.setCourierId(courierId);
		demandOrderMessageVo.setArriveTime(arriveTime);
		demandOrderMessageVo.setCourierName(courierName);
		demandOrderMessageVo.setOrderCourierState(courierOrders.get(0)
				.getOrderCourierState());
		return demandOrderMessageVo;
	}

	/**
	 * 通过快递员id获取快递员姓名
	 * 
	 * @param courierId
	 * @return
	 */
	public String getCourierName(int courierId) {

		int userId = userCourierMapper.selectByPrimaryKey(courierId)
				.getUserId();

		String courierName = userInfomationMapper.selectByPrimaryKey(userId)
				.getNickname();

		return courierName;
	}

	/**
	 * 通过用户id，复制快递员信息
	 * 
	 * @param userId
	 * @return
	 */
	public UserInfomationShowVo getCourierMessage(int userId,
			UserInfomationShowVo userInfomationShowVo) {

		UserInfomation userInfomation = userInfomationMapper
				.selectByPrimaryKey(userId);
		int permissions = userInfomation.getPermissions();
		if (permissions == 1) {
			UserCourierExample example = new UserCourierExample();
			example.or().andUserIdEqualTo(userId);
			List<UserCourier> userCouriers = userCourierMapper
					.selectByExample(example);
			if (userCouriers.get(0).getUserCourierState() == 1) {
				BeanUtils.copyProperties(userCouriers.get(0), userInfomationShowVo);
			}
		}

		return userInfomationShowVo;
	}

	@Override
	public UserAddressVo getAddressMessage(int addressId) {
		UserAddressVo userAddressVo = new UserAddressVo();
		UserAddress userAddress = userAddressMapper
				.selectByPrimaryKey(addressId);
		BeanUtils.copyProperties(userAddress, userAddressVo);
		return userAddressVo;
	}

	@Override
	public String headPortraitUpload(MultipartFile file, String path, int userId) {

		String fileName = "mobile/image/" + UUID.randomUUID().toString()
				+ ".jpg";
		String saveFileName = "/randomdelivery/" + fileName;
		String newFileName = path + fileName;

		try {
			file.transferTo(new File(newFileName));
			UserInfomation userInfomation = new UserInfomation();
			userInfomation.setUserId(userId);
			userInfomation.setHeadPortraitUrl(saveFileName);
			userInfomationMapper.updateByPrimaryKeySelective(userInfomation);

			return saveFileName;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public String imageUpload(MultipartFile file, String path,int temp) {
		String fileName = "mobile/image/" + UUID.randomUUID().toString()
				+ ".jpg";
		String saveFileName = "/randomdelivery/" + fileName;
		String newFileName = path + fileName;
		if(temp == 0){
			try {
				file.transferTo(new File(newFileName));
				ImageUrlDemandOrder imageUrlDemandOrder = new ImageUrlDemandOrder();
				imageUrlDemandOrder.setImageUrl(saveFileName);
				imageUrlDemandOrderMapper.insertSelective(imageUrlDemandOrder);

				return saveFileName;
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if(temp == 1){
			try {
				file.transferTo(new File(newFileName));
				ImageUrlDemandOrderStore imageUrlDemandOrderStore = new ImageUrlDemandOrderStore();
				imageUrlDemandOrderStore.setStoreImageUrl(saveFileName);
				imageUrlDemandOrderStoreMapper.insertSelective(imageUrlDemandOrderStore);
				return saveFileName;
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	
	
	}

	@Override
	public RefundReasonShowVo lookRefund(OrderIdVo orderIdVo) {
		RefundReasonShowVo refundReasonShowVo = new RefundReasonShowVo();
		
		UserRefundExample userRefundExample = new UserRefundExample();
		userRefundExample.or().andOrderIdEqualTo(orderIdVo.getOrderId());
		List<UserRefund> userRefunds = userRefundMapper.selectByExample(userRefundExample);
			
		BeanUtils.copyProperties(userRefunds.get(0), refundReasonShowVo);
		
		return refundReasonShowVo;
	}

	@Override
	public void changePassword(int userId, String password) {
		UserLogin userLogin = new UserLogin();
		userLogin.setUserId(userId);
		userLogin.setPassword(password);
		userLoginMapper.updateByPrimaryKeySelective(userLogin);
	}

}
